/*
 * UnitCostAnalyzer.h
 *
 *  Created on: Nov 21, 2008
 *      Author: Ryan
 */

#ifndef UNITCOSTANALYZER_H_
#define UNITCOSTANALYZER_H_

#include "../../../OOInterface/Interfaces/UnitVisitor.h"
#include "../../../OOInterface/Objects/Units/Unit.h"

typedef std::map< const OOInterface::Resource, float> ResourceMap;

class UnitCostAnalyzer : public OOInterface::UnitVisitor
{
public:
	UnitCostAnalyzer( const OOInterface::ResourceList types ) : _resourceTypes( types ) {}
	virtual ~UnitCostAnalyzer() {}

	virtual void visitUnit( const OOInterface::Unit& u )
	{
		for( OOInterface::ResourceList::const_iterator resource = _resourceTypes.begin(); resource != _resourceTypes.end(); ++resource )
			_cost.insert( std::pair< const OOInterface::Resource, float >( *resource, u.getResourceCost( *resource ) ) );
	}

	virtual void visitAirAspect( const OOInterface::AirAspect& a ){}
	virtual void visitLandAspect( const OOInterface::LandAspect& a ){}
	virtual void visitWaterAspect( const OOInterface::WaterAspect& a ){}

	virtual void visitBuilderAspect( const OOInterface::BuilderAspect& a ) {}
	virtual void visitBuildingAspect( const OOInterface::BuildingAspect& a ) {}
	virtual void visitTransportAspect( const OOInterface::TransportAspect& a ){}

	virtual void visitReclaimableAspect( const OOInterface::ReclaimableAspect& a ){}
	virtual void visitResourceExtractorAspect( const OOInterface::ResourceExtractorAspect& a ) {}

	void reset() { _cost = ResourceMap(); }
	ResourceMap getCost() { return _cost; }

	bool canAfford( ResourceMap available )
	{
		for( OOInterface::ResourceList::const_iterator resource = _resourceTypes.begin(); resource != _resourceTypes.end(); ++resource )
		{
			ResourceMap::iterator val1 = available.find( *resource );
			ResourceMap::iterator val2 = _cost.find( *resource );

			if( val2 == _cost.end())
				continue;

			if( val1 == available.end() )
			{
				return false;
			}

			if( val2->second > val1->second )
			{
				return false;
			}

		}
		return true;
	}

private:
	ResourceMap _cost;

	const OOInterface::ResourceList _resourceTypes;
};

#endif /* UNITCOSTANALYZER_H_ */
